package org.dizem.intellipainter.component.color;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;

/**
 * User: dizem@126.com
 * Time: 12-2-18 下午3:18
 */
public class ColorPickerBox extends JPanel {
	private static final int[] DEFAULT_COLORS = new int[]{
			0x000000, 0x7F7F7F, 0x88015, 0xEDEC24, 0xFF7F27, 0xFFF20, 0x22B14C, 0x0A2E8, 0x3F48CC, 0xA349A4,
			0xFFFFFF, 0xC3C3C3, 0xB97A57, 0xFFAEC9, 0xFFC9E, 0xEFE4B0, 0xB5E61D, 0x99D9EA, 0x7092BE, 0xC8BFE7
	};
	private static final int COLUMNS = 10;
	private static final int ROWS = 3;
	private final List<ColorLabel> customColorLabels = new ArrayList<ColorLabel>();
	private int cursor = 0;

	public ColorPickerBox(ColorPickCallback callback) {
		super(true);
		setLayout(new GridLayout(ROWS, COLUMNS, 3, 3));

		for (int row = 0; row < ROWS; ++row) {
			for (int column = 0; column < COLUMNS; ++column) {
				int index = row * COLUMNS + column;
				if (DEFAULT_COLORS.length > index) {
					add(new ColorLabel(DEFAULT_COLORS[index], callback));
				} else {
					ColorLabel label = new ColorLabel(Color.WHITE, callback);
					label.setEnabled(false);
					customColorLabels.add(label);
					add(label);
				}
			}
		}
	}


	public void addColor(Color color) {
		if (color != null) {
			customColorLabels.get(cursor).setColor(color);
			customColorLabels.get(cursor).setEnabled(true);
			cursor = (cursor + 1) % customColorLabels.size();
		}
	}
}
